﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Game2048WithCSharp
{
    internal partial class UIController: IUser
    {
        private UIElements uiElements;
        private CubeMoveController cubeMoveController = new CubeMoveController();
        private List<Cube> cubes = new List<Cube>();
        public void Init(UIElements uiElements)
        {
            this.uiElements = uiElements;
            uiElements.UpdateTimer.Tick += UpdateTimer_Tick;
            cubeMoveController.Init(uiElements.removeControl, CheckGameState, cubes,
                uiElements.StartTask, uiElements.EndTask, GenerateNewCubeInTask);
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            double DeltaTime = uiElements.UpdateTimer.Interval / (double)1000;
            cubeMoveController.TimeUpdate(DeltaTime);
        }

        public void NewGame(int[,] chessBoard, int currentScore, int bestScore)
        {
            uiElements.currentScoreLabel.Text = currentScore.ToString();
            uiElements.bestScoreLabel.Text = bestScore.ToString();
            ClearChessBoard();
            cubeMoveController.Clear();
            for (int i = 0; i < chessBoard.GetLength(0); i++)
            {
                for (int j = 0; j < chessBoard.GetLength(1); j++)
                {
                    if (chessBoard[i, j] != 0)
                    {
                        Cube cube = new Cube(chessBoard[i, j], i, j);
                        cubes.Add(cube);
                        uiElements.addControl(cube);
                        cube.BringToFront();
                    }
                }
            }
            CheckGameState(GameState.Gaming);
        }
        public void MoveCubes(CubeMove[] cubeMoves, GameState gameState, int currentScore, int bestScore)
        {
            //Debug.WriteLine("Move Cubes");
            //foreach (CubeMove cubeMove in cubeMoves)
            //{
            //    Debug.WriteLine(string.Format("Cube Move From ({0}, {1}) to ({2}, {3})", cubeMove.OldX, cubeMove.OldY, cubeMove.NewX, cubeMove.NewY));
            //}
            uiElements.currentScoreLabel.Text = currentScore.ToString();
            uiElements.bestScoreLabel.Text = bestScore.ToString();
            cubeMoveController.AddCubeMoveTask(cubeMoves, gameState);
        }
        public void GenerateNewCubeInTask(int[,] newCube, GameState gameState)
        {
            for (int i = 0; i < newCube.GetLength(0); i++)
            {
                for (int j = 0; j < newCube.GetLength(1); j++)
                {
                    if (newCube[i, j] != 0)
                    {
                        Cube cube = new Cube(newCube[i, j], i, j);
                        cubes.Add(cube);
                        uiElements.addControl(cube);
                        cube.BringToFront();
                    }
                }
            }
            CheckGameState(gameState);
        }
        public void GenerateNewCube(int[,] newCube, GameState gameState)
        {
            cubeMoveController.AddGenerateNewCubeTask(newCube, gameState);
        }
        internal void CheckGameState(GameState gameState)
        {
            Debug.WriteLine("Check Game State: " + gameState.ToString());
            switch (gameState)
            {
                case GameState.Gaming:
                    {
                        uiElements.VictoryLabel.Visible = false;
                        uiElements.DefeatLabel.Visible = false;
                        break;
                    }
                case GameState.Victory:
                    {
                        uiElements.VictoryLabel.BringToFront();
                        uiElements.VictoryLabel.Visible = true;
                        uiElements.DefeatLabel.Visible = false;
                        break;
                    }
                case GameState.Defeat:
                    {
                        uiElements.VictoryLabel.Visible = false;
                        uiElements.DefeatLabel.BringToFront();
                        uiElements.DefeatLabel.Visible = true;
                        break;
                    }
            }
        }
        private void ClearChessBoard()
        {
            foreach (Cube cube in cubes)
            {
                cube.Visible = false;
                uiElements.removeControl(cube);
            }
            cubes.Clear();
        }
    }
}
